package com.yongqi.wxAPI;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yongqi.model.Family;
import com.yongqi.model.Member;
import com.yongqi.model.console.Bill;
import com.yongqi.pub.DateFormat;
import com.yongqi.service.FamilyService;
import com.yongqi.service.MemberService;
import com.yongqi.service.console.BillService;
import com.yongqi.service.console.IncomeService;
import com.yongqi.service.console.PayService;

@Controller
@RequestMapping("/wx")
public class WXInterface {
	@Autowired
	FamilyService familyService;
	@Autowired
	BillService billService;
	@Autowired
	MemberService memberService;
	@Autowired
	IncomeService incomeService;
	@Autowired
	PayService payService;
	/**
	 * 微信小程序登录
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public String wxLogin(HttpServletRequest request) {
		String account = request.getParameter("familyaccount");
		String password = request.getParameter("password");
		password = new SimpleHash("MD5",password,account,2).toHex();
		Family family = familyService.getFamilyByAccount(account);
		JSONObject object = new JSONObject();
		if (family == null) {
			object.put("result", "usernamenotfound");
		}else{
			if (family.getFamilypassword().equals(password)) {
				object.put("result", "success");
			}else {
				object.put("result", "passwordwrong");
			}
		}
		System.out.println(object.toJSONString());
		return object.toJSONString();
	}
	
	/**
	 * 微信小程序获取当前用户账单信息
	 */
	@ResponseBody
	@RequestMapping("/bill")
	public String wxIncome(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("familyAccount", request.getParameter("familyaccount"));
		map.put("billType", request.getParameter("billType"));
 		List<Bill> income = billService.queryPageItems(map);
 		JSONObject object = new JSONObject();
 		object.put("income", income);
 		return object.toJSONString();
	}
	
	/**
	 * 微信小程序获取当前用户成员信息
	 */
	@ResponseBody
	@RequestMapping("/member")
	public String wxMember(HttpServletRequest request) {
		System.out.println(request.getParameter("familyaccount"));
		Map<String, String> map = new HashMap<String, String>();
		map.put("familyAccount", request.getParameter("familyaccount"));
		List<Member> members = memberService.memberQuery(map);
 		List<String> membersList = new ArrayList<String>();
 		for (int i = 0; i < members.size(); i++) {
 			membersList.add(members.get(i).getMemberName());        
		}
 		JSONObject object = new JSONObject();
 		object.put("membersList", membersList);
 		return object.toJSONString();
	}
	
	/**
	 * 微信小程序添加收入信息
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/incomeadd")
	public String wxIncomeadd(HttpServletRequest request) {
		Bill income = new Bill();
		String familyAccount = request.getParameter("familyaccount");
		income.setId(UUID.randomUUID().toString());
		income.setFamilyAccount(familyAccount);
		income.setMemberAccount(familyAccount);
		income.setDate(request.getParameter("date"));
		income.setItem(request.getParameter("item"));
		income.setTip(request.getParameter("tip"));
		income.setType(request.getParameter("type"));
		income.setTypeDetail(request.getParameter("typeDetail"));
		income.setMember(request.getParameter("member"));
		income.setMoney(request.getParameter("money"));
		income.setBillType("income");
		income.setCreated_time(DateFormat.dateFormate());
		incomeService.incomeAdd(income);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
	
	/**
	 * 微信小程序添加支出信息
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@RequestMapping("/payadd")
	public String addItem(HttpServletRequest request) throws SQLException {
		Bill pay = new Bill();
		String familyAccount = request.getParameter("familyaccount");
		pay.setId(UUID.randomUUID().toString());
		pay.setFamilyAccount(familyAccount);
		pay.setMemberAccount(familyAccount);
		pay.setDate(request.getParameter("date"));
		pay.setItem(request.getParameter("item"));
		pay.setTip(request.getParameter("tip"));
		pay.setType(request.getParameter("type"));
		pay.setTypeDetail(request.getParameter("typeDetail"));
		pay.setMember(request.getParameter("member"));
		pay.setMoney(request.getParameter("money"));
		pay.setBillType("pay");
		pay.setCreated_time(DateFormat.dateFormate());
		payService.payAdd(pay);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
}
